package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.db.domain.MarketCouponUserExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

public class AdminCouponServiceImpl implements AdminCouponService {

    @Override
    public int delete(Integer id) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);
        int i = couponMapper.logicalDeleteByPrimaryKey(id);
        session.commit();
        session.close();
        return i;
    }

    @Override
    public List<MarketCoupon> list(String page, String limit, String name, String type, String status, String sort, String order) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(type)) {
            criteria.andTypeEqualTo(Short.parseShort(type));
        }
        if (!StringUtils.isEmpty(status)) {
            criteria.andStatusEqualTo(Short.parseShort(status));
        }
        criteria.andDeletedEqualTo(false);
        marketCouponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketCoupon> couponList = couponMapper.selectByExample(marketCouponExample);

        session.commit();
        session.close();
        return couponList;
    }

    @Override
    public MarketCoupon update(MarketCoupon marketCoupon) {
        marketCoupon.setUpdateTime(LocalDateTime.now());

        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper couponMapper = session.getMapper(MarketCouponMapper.class);

        couponMapper.updateByPrimaryKey(marketCoupon);
        session.commit();
        session.close();

        return marketCoupon;
    }

    @Override
    public List<MarketCouponUser> listuser(String page, String limit, String couponId, String userId,String status,String sort, String order) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponUserMapper marketCouponUserMapper = session.getMapper(MarketCouponUserMapper.class);

        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.or().andCouponIdEqualTo(Integer.parseInt(couponId));
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtils.isEmpty(status)) {
            criteria.andStatusEqualTo(Short.parseShort(status));
        }
        criteria.andDeletedEqualTo(false);
        marketCouponUserExample.setOrderByClause(sort+" "+order);

        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);

        session.commit();
        session.close();

        return marketCouponUsers;
    }

    @Override
    public MarketCoupon read(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(Integer.parseInt(id));

        session.commit();
        session.close();

        return marketCoupon;
    }

    @Override
    public MarketCoupon insert(MarketCoupon marketCoupon) {
        marketCoupon.setAddTime(LocalDateTime.now());
        marketCoupon.setUpdateTime(LocalDateTime.now());

        SqlSession session = MybatisUtil.getSession();
        MarketCouponMapper marketCouponMapper = session.getMapper(MarketCouponMapper.class);
        marketCouponMapper.insertSelective(marketCoupon);

        session.commit();
        session.close();

        return marketCoupon;
    }
}
